//
//  RGoodController.m
//  EPAa
//
//  Created by lanou3g on 16/4/23.
//  Copyright © 2016年 李日青. All rights reserved.
//

#import "RGoodController.h"
#import "RNetWorkRequestManager.h"
#import "RGoodModel.h"
#import "RGoodViewCell.h"
#import "RGoodDetailController.h"
#import "RtaobaoController.h"
@interface RGoodController ()<CheangGoodDidCliock>
@property (nonatomic, strong)NSMutableArray *goodArray;
@end

@implementation RGoodController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerNib:[UINib nibWithNibName:@"RGoodViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    self.goodArray = [NSMutableArray new];
    [self readJson];
    

}

- (void)readJson {
    [RNetWorkRequestManager requestURL:[NSURL URLWithString:SHOPLIST_URL] Method:CK_POST Params:@{@"start":@"0",@"client":@"1",@"deviceid":@"63A94D37-33F9-40FF-9EBB-481182338873",@"limit":@"10",@"auth":@"",@"version":@"3.0.2"} Result:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (data) {
            NSMutableDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSLog(@"%@",jsonDict);
            
            NSDictionary *dataDic = jsonDict[@"data"];
            for (NSDictionary *itemDict in dataDic[@"list"]) {
                RGoodModel *model = [RGoodModel new];
                [model setValuesForKeysWithDictionary:itemDict];
                [self.goodArray addObject:model];
            }
          dispatch_async(dispatch_get_main_queue(), ^{
              [self. tableView reloadData];
          });
        }
    }];
    
}

- (void)didDataChengValue:(NSString *)Url{
    
    RtaobaoController *taobaoVC = [RtaobaoController new];
    taobaoVC.taobaoUrl = Url;
    [self.navigationController pushViewController:taobaoVC animated:YES];
    NSLog(@"%@",Url);
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return self.goodArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    RGoodViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.model = self.goodArray[indexPath.row];
    
    cell.goodDelge = self;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 230;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    RGoodDetailController *detailVC = [RGoodDetailController new];
    RGoodModel *model = self.goodArray[indexPath.row];
    detailVC.coverStr = model.coverimg;
    [self.navigationController pushViewController:detailVC animated:YES];
    
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
